
//
//  HMStatusOriginalView.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/17.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMStatusOriginalView.h"
#import "HMStatusOriginalFrame.h"
#import "HMStatus.h"
#import "HMUser.h"
#import "HMStatusPhotosView.h"

@interface HMStatusOriginalView()

//头像
@property (nonatomic, weak) UIImageView *iconImageView;
//昵称
@property (nonatomic, weak) UILabel *nameLabel;
//会员图标
@property (nonatomic, weak) UIImageView *vipImageView;
//时间
@property (nonatomic, weak) UILabel *timeLabel;
//来源
@property (nonatomic, weak) UILabel *sourceLabel;
//正文
@property (nonatomic, weak) UILabel *textLabel;
//图片
@property (nonatomic, strong) HMStatusPhotosView *photosView;

@end


@implementation HMStatusOriginalView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        
        [self setupView];
    }
    return self;
}

- (void)setupView{

    //1.头像
    UIImageView *iconImageView = [[UIImageView alloc] init];
    [self addSubview:iconImageView];
    self.iconImageView = iconImageView;
    
    //2.昵称
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = HMStatusOriginalNameFont;
    [self addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    //3.会员图标
    UIImageView *vipImageView = [[UIImageView alloc] init];
    [self addSubview:vipImageView];
    self.vipImageView = vipImageView;
    
    //4.时间
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = HMStatusOriginalTimeFont;
    timeLabel.textColor = [UIColor orangeColor];
    [self addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    //5.来源
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.font = HMStatusOriginalSourceFont;
    sourceLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    //6.正文
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.font = HMStatusOriginalTextFont;
    textLabel.numberOfLines = 0;
    [self addSubview:textLabel];
    self.textLabel = textLabel;
    
    //7.图片
    HMStatusPhotosView *photosView = [[HMStatusPhotosView alloc] init];
    [self addSubview:photosView];
    self.photosView = photosView;
    
}


//根据传进来的微博数据计算所有控件的frame
//重写 originalFrame 的setter 方法
- (void)setOriginalFrame:(HMStatusOriginalFrame *)originalFrame{
    _originalFrame = originalFrame;
    
    self.frame = originalFrame.originalF;
    
    //取出微博数据
    HMStatus *status = originalFrame.status;
    //取出用户数据
    HMUser *user = status.user;
    
    //1.昵称
    self.nameLabel.text = user.name;
    self.nameLabel.frame = originalFrame.nameFrame;
    
    //2.会员
    //如果是会员
    if (user.isVip) {
        self.nameLabel.textColor = [UIColor orangeColor];
        self.vipImageView.hidden = NO;
        self.vipImageView.frame = originalFrame.vipFrame;
        //下面这句代码作用是:无论vipImageView的frame多大,vip图标都以图标原有尺寸居中显示
        self.vipImageView.contentMode = UIViewContentModeCenter;
        self.vipImageView.image = [UIImage imageWithName:[NSString stringWithFormat:@"common_icon_membership_level%d", user.mbrank]];
    }else{
      //不是会员
        self.vipImageView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }
    
    //3.正文
    self.textLabel.text = status.text;
    self.textLabel.frame = originalFrame.textFrame;
    
    //4.时间  时刻根据新的时间来计算frame
    NSString *timeStr = status.created_at;
    self.timeLabel.text = timeStr;
    CGFloat timeX = CGRectGetMinX(self.nameLabel.frame);
    CGFloat timeY = CGRectGetMaxY(self.nameLabel.frame) + HMStatusCellInset * 0.5;
    CGSize timeSize = [timeStr sizeWithAttributes:@{NSFontAttributeName:HMStatusOriginalTimeFont}];
    self.timeLabel.frame = (CGRect){{timeX, timeY}, timeSize};
    
    //5.来源 因为时间改变,导致时间导致时间的frame 改变,导致来源的frame改变
    //getter 只要滚动cell,时刻在调用 耗内存
    //setter 在模型-字典 赋值那一刻 每个模型只调用一次
    //因为时间时时刻刻在改变,所以时间可以时时刻刻重新赋值,重新计算frame,但是来源是不会改变的,所以只需调用一次即可
    NSString *sourceStr = status.source;
    self.sourceLabel.text = sourceStr;
    
    CGFloat sourceX = CGRectGetMaxX(self.timeLabel.frame) + HMStatusCellInset;
    CGFloat sourceY = timeY;
    CGSize sourceSize = [sourceStr sizeWithAttributes:@{NSFontAttributeName:HMStatusOriginalSourceFont}];
    self.sourceLabel.frame = (CGRect){{sourceX, sourceY}, sourceSize};
    
    //6.头像
    self.iconImageView.frame = originalFrame.iconFrame;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
    
    //7.图片
    if (status.pic_urls.count) {
        //如果有图片,设置其frame,且其隐藏属性 为NO
        self.photosView.frame = originalFrame.photosFrame;
        self.photosView.pic_urls = status.pic_urls;
        self.photosView.hidden = NO;
        
    }else{
        //如果没有图片,其隐藏属性 为YES
        self.photosView.hidden = YES;
        
    }
    
    
}


@end
